--[[
  江西银行 AFANATP - 7003端口
]]
require("apm_core")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local dict_channelname = {
    ["EUB"] = "VTM",
    ["ATM"] = "本行ATM",
    ["BCK"] = "后台",
    ["CAL"] = "电话人工",
    ["CDS"] = "自助取款机",
    ["CPR"] = "合作方",
    ["CUA"] = "中国银联ATM",
    ["CUP"] = "中国银联POS",
    ["DSK"] = "柜面",
    ["INT"] = "个人网上银行",
    ["PAY"] = "电子支付",
    ["POS"] = "本行POS",
    ["STM"] = "自助终端",
    ["TEL"] = "电话语音",
    ["MPH"] = "手机银行",
}

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getMsgType() ~= 1 then return end

    local s_transcode
    local s_transname
    local s_channelcode
    local s_channelname
    local s_flowno
    local s_rspcode
    local s_rspmsg

    -- 交易类型
    s_transcode = ApmData.getField("request.jxbank_afanatp.G_TRANSCODE", 1)
    if (s_transcode == nil or s_transcode == "") and ApmData.getField("transaction.type_code", 1) == "SZTRANSM" then
        s_transcode = ApmData.getField("transaction.type_code", 1)
    end

    -- 渠道类型
    s_channelcode = ApmData.getField("request.jxbank_afanatp.Channel", 1)
    if s_channelcode == nil or s_channelcode == "" then
        s_channelcode = ApmData.getField("request.jxbank_afanatp.gsreqroot_Channel", 1)
    end
    if s_channelcode ~= nil and s_channelcode ~= "" then
        s_channelname = dict_channelname[s_channelcode]
        if s_channelname == nil then s_channelname = s_channelcode end
    end

    -- 返回码
    s_rspcode = ApmData.getField("response.jxbank_afanatp.bankerrcode", 1)
    s_rspmsg = ApmData.getField("response.jxbank_afanatp.bankerrmsg", 1)
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.jxbank_afanatp.MsgCode", 1)
    end
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.jxbank_afanatp.bankdealcode", 1)
        s_rspmsg = ApmData.getField("response.jxbank_afanatp.bankdealmsg", 1)
    end
    if s_rspcode == nil then
        s_rspcode = ApmData.getField("response.jxbank_afanatp.errorcode", 1)
        s_rspmsg = ApmData.getField("response.jxbank_afanatp.errormsg", 1)
    end

    if s_transcode == nil or s_transcode == "" then
        logInfo("s_transcode is nil [%s]", ApmData.dumpFields(true))
--    elseif s_channelcode == nil then
--        logInfo("s_channelcode is nil [%s]", ApmData.dumpFields(true))
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil [%s]", ApmData.dumpFields(true))
    else
        ApmData.replaceField("transaction.type_code", s_transcode)
        ApmData.replaceField("transaction.type_name", s_transcode)
        ApmData.replaceField("transaction.title", s_transcode)

        if s_channelcode ~= nil and s_channelcode ~= "" then
            ApmData.replaceField("transaction.channel_code", s_channelcode)
            ApmData.replaceField("transaction.channel_name", s_channelname)
        end

        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        logInfo("AFANATP 7003(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            ApmData.getDstIp(),
            ApmData.getDstPort(),
            s_transcode,
            s_channelname,
            s_flowno,
            s_rspcode,
            s_rspmsg
        )
    end

    return
end
--AFANATP
--10.1.1.124:7003
--10.1.1.125:7003
--10.1.1.126:7003
--v1.0.1
